package org.smart4j.chapter1.plugin.security;

import org.smart4j.chapter1.helper.DatabaseHelper;

import java.util.Set;

/**
 * @ClassName:
 * @Description: 应用安全控制
 * @Author: LynnZou
 * @Date: 2017/5/31
 */
public class AppSecurity implements SmartSecurity {

    @Override
    public String getPassword(String username) {
        String sql = "SELECT password FROM user WHERE username = ? ";
        return DatabaseHelper.query(sql,username);
    }

    @Override
    public Set<String> getRoleNameSet(String username) {
        String sql = "select r.role_name from user u,user_role ur,role r where u.id = ur.user_id and r.id = ur.role_id and u.username = ? ";
        return DatabaseHelper.querySet(sql,username);
    }

    @Override
    public Set<String> getPermissionNameSet(String roleName) {
        String sql = "select p.permission_name from role r, role_permission rp,permission p where r.id = rp.role_id and p.id = rp.permission_id and r.role_name = ? ";
        return DatabaseHelper.querySet(sql,roleName);
    }
}
